package com.example.content.views

import android.content.Context
import android.view.View
import android.view.animation.Animation
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.content.R
import com.example.content.SHARE_PERSONAL_CARD_TYPE
import com.example.content.adapter.recycle.BindListener
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.entity.ItemEntity
import com.example.content.adapter.recycle.entity.ShareBean
import com.example.content.manager.ActivityMgr
import com.example.core.extensions.showToast
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/12
 */
class ShareMyCardPopup(val context: Context) : BasePopupWindow(context) {

    override fun onCreateContentView(): View {
        val sharePop = createPopupById(R.layout.share_card_pop_layout)
        val recyclerView = sharePop.findViewById<RecyclerView>(R.id.recycler_view)
        val recyclerAdapter = RecyclerAdapter(object : BindListener<ShareBean> {
            override fun convert(holder: BaseViewHolder, item: ItemEntity<ShareBean>) {
                val shareIcon = holder.getView<ImageView>(R.id.share_icon)
                shareIcon.setImageResource(item.data.imageResId)
                val title = holder.getView<TextView>(R.id.share_title)
                title.text = item.data.title
            }
        })
        recyclerAdapter.addItemView(R.layout.item_share_list_layout)
        for (i in share_titles.indices) {
            recyclerAdapter.data.add(
                ItemEntity(
                    data = ShareBean(
                        share_icons[i],
                        getContext().getString(share_titles[i])
                    )
                )
            )
        }
        recyclerAdapter.setOnItemClickListener { _, _, position ->
            context.showToast("点击了 ${recyclerAdapter.data[position].data.title} 分享")
        }
        recyclerView.adapter = recyclerAdapter
        recyclerView.layoutManager =
            LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false)
        val shareCard = sharePop.findViewById<View>(R.id.share_card)
        val shareCardImage = shareCard.findViewById<ImageView>(R.id.share_card_image)
        val shareCardDot = shareCard.findViewById<ImageView>(R.id.share_card_dot)
        val shareLink = sharePop.findViewById<View>(R.id.share_link)
        val shareLinkDot = shareLink.findViewById<ImageView>(R.id.share_link_dot)
        val shareCardListener = View.OnClickListener {
            shareCardDot.setImageResource(R.drawable.ic_dot_checked)
            shareCard.isSelected = true
            shareLinkDot.setImageResource(R.drawable.ic_card_dot_grey)
            shareLink.isSelected = false
        }

        val shareLinkListener = View.OnClickListener {
            shareCardDot.setImageResource(R.drawable.ic_card_dot_grey)
            shareCard.isSelected = false
            shareLinkDot.setImageResource(R.drawable.ic_dot_checked)
            shareLink.isSelected = true
        }
        shareCard.isSelected = true
        shareCard.setOnClickListener(shareCardListener)
        shareCardDot.setOnClickListener(shareCardListener)
        shareLink.setOnClickListener(shareLinkListener)
        shareLinkDot.setOnClickListener(shareLinkListener)
        shareCardImage.setOnClickListener {
            ActivityMgr.startFragment(SHARE_PERSONAL_CARD_TYPE)
        }
        val cancelBtn = sharePop.findViewById<Button>(R.id.cancel)
        cancelBtn.setOnClickListener {
            this.dismiss()
        }
        return sharePop
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

    companion object {
        val share_icons = arrayListOf(
            R.drawable.share_weixin, R.drawable.share_pyq, R.drawable.share_weibo,
            R.drawable.share_qq, R.drawable.share_qq_zone
        )
        val share_titles = arrayListOf(
            R.string.share_weixin, R.string.share_pyq,
            R.string.share_weibo, R.string.share_qq, R.string.share_qq_zone
        )
    }
}